const Router = require('@koa/router');
const { checkAuth, checkPermission } = require('../middlewares/auth');
const cacheController = require('../controllers/cache.controller');

const router = new Router({ prefix: '/api/v1/cache' });

/**
 * @swagger
 * /api/v1/cache/{key}:
 *   get:
 *     tags: [缓存管理]
 *     summary: 获取缓存值
 *     description: 获取指定键的缓存值
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: key
 *         required: true
 *         schema:
 *           type: string
 *         description: 缓存键名
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 value:
 *                   type: object
 *                   description: 缓存的值
 *                 ttl:
 *                   type: integer
 *                   description: 剩余生存时间（秒）
 *                   example: 3600
 *       404:
 *         description: 缓存键不存在
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.get('/:key', checkAuth, checkPermission('cache:read'), cacheController.get);

/**
 * @swagger
 * /api/v1/cache/{key}:
 *   put:
 *     tags: [缓存管理]
 *     summary: 设置缓存值
 *     description: 设置指定键的缓存值
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: key
 *         required: true
 *         schema:
 *           type: string
 *         description: 缓存键名
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - value
 *             properties:
 *               value:
 *                 type: object
 *                 description: 要缓存的值
 *                 example: { "data": "示例数据" }
 *               ttl:
 *                 type: integer
 *                 description: 生存时间（秒）
 *                 example: 3600
 *     responses:
 *       200:
 *         description: 设置成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "缓存设置成功"
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.put('/:key', checkAuth, checkPermission('cache:write'), cacheController.set);

/**
 * @swagger
 * /api/v1/cache/{key}:
 *   delete:
 *     tags: [缓存管理]
 *     summary: 删除缓存值
 *     description: 删除指定键的缓存值
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: key
 *         required: true
 *         schema:
 *           type: string
 *         description: 缓存键名
 *     responses:
 *       200:
 *         description: 删除成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "缓存删除成功"
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 缓存键不存在
 */
router.delete('/:key', checkAuth, checkPermission('cache:delete'), cacheController.delete);

/**
 * @swagger
 * /api/v1/cache/{key}/exists:
 *   get:
 *     tags: [缓存管理]
 *     summary: 检查缓存键
 *     description: 检查指定的缓存键是否存在
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: key
 *         required: true
 *         schema:
 *           type: string
 *         description: 缓存键名
 *     responses:
 *       200:
 *         description: 检查成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 exists:
 *                   type: boolean
 *                   example: true
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.get('/:key/exists', checkAuth, checkPermission('cache:read'), cacheController.exists);

/**
 * @swagger
 * /api/v1/cache:
 *   delete:
 *     tags: [缓存管理]
 *     summary: 清空缓存
 *     description: 清空所有缓存数据
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 清空成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "缓存已清空"
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.delete('/', checkAuth, checkPermission('cache:delete'), cacheController.clear);

/**
 * @swagger
 * /api/v1/cache/hash/{hash}/{key}:
 *   get:
 *     tags: [缓存管理]
 *     summary: 获取哈希值
 *     description: 获取指定哈希表中键的值
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: hash
 *         required: true
 *         schema:
 *           type: string
 *         description: 哈希表名称
 *       - in: path
 *         name: key
 *         required: true
 *         schema:
 *           type: string
 *         description: 哈希表键名
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 value:
 *                   type: object
 *                   description: 哈希表中的值
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 哈希表或键不存在
 */
router.get('/hash/:hash/:key', checkAuth, checkPermission('cache:read'), cacheController.getHash);

/**
 * @swagger
 * /api/v1/cache/hash/{hash}/{key}:
 *   put:
 *     tags: [缓存管理]
 *     summary: 设置哈希值
 *     description: 在指定哈希表中设置键值对
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: hash
 *         required: true
 *         schema:
 *           type: string
 *         description: 哈希表名称
 *       - in: path
 *         name: key
 *         required: true
 *         schema:
 *           type: string
 *         description: 哈希表键名
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - value
 *             properties:
 *               value:
 *                 type: object
 *                 description: 要存储的值
 *                 example: { "data": "示例数据" }
 *     responses:
 *       200:
 *         description: 设置成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "哈希值设置成功"
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.put('/hash/:hash/:key', checkAuth, checkPermission('cache:write'), cacheController.setHash);

/**
 * @swagger
 * /api/v1/cache/hash/{hash}/{key}:
 *   delete:
 *     tags: [缓存管理]
 *     summary: 删除哈希值
 *     description: 删除指定哈希表中的键值对
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: hash
 *         required: true
 *         schema:
 *           type: string
 *         description: 哈希表名称
 *       - in: path
 *         name: key
 *         required: true
 *         schema:
 *           type: string
 *         description: 哈希表键名
 *     responses:
 *       200:
 *         description: 删除成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "哈希值删除成功"
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 哈希表或键不存在
 */
router.delete('/hash/:hash/:key', checkAuth, checkPermission('cache:delete'), cacheController.deleteHash);

module.exports = router; 